from socket import *
from multiprocessing import Process
from dict_db import *

#处理请求
class Handle(Process):
    def __init__(self,conn):
        self.conn=conn
        super.__init__()
        self.db=DictData()  #数据库处理对象客户端退出，进程结束，连接关闭

    #具体处理客户端请求,总分结构，总体接收请求，分情况讨论
    def run(self):
        #总体接收
        while True:
            request=self.conn.recv(1024)
            print(request)

        self.conn.close()
        self.db.close()

        cmd=request



        #分情况讨论
            if not request :
                break
            if request=="register":
                self.register()
            elif request=="L":
                self.enter()
            else: request==""
    #注册
    def register(self):
        pass
    #登录
    def enter(self):
        pass
    #退出




class DICTserver:
    #创建实例变量
    def __init__(self,host="",port=0):
        self.host = host
        self.port = port
        self.address = (host, port)
        self.sock = self.create_socket()
    def create_socket(self):
        sock = socket( AF_INET, SOCK_STREAM )
        sock.bind( self.address )
        return sock

    #启动服务
    def server_forever(self):
        self.sock.listen(5)
        print("listen the port %d" % self.port) #每当有一个客户端进来，就创建一个新的进程为其服务
        #循环接受请求
        while True:
            conn,addr=self.sock.accept()
            print("connect from",addr)
            #自定义进程类
            p=Handle(conn)
            p.start()


if __name__ == '__main__':
    dict=DICTserver(host="0.0.0.0",port=8888)
    dict.server_forver() #启动服务



















while True:
    print("Waiting for connect...")
    connfd,addr = tcp_socket.accept()
    print("Connect from",addr)

    while True:
        data = connfd.recv(5)

        if not data or data == b'##':
            break
        print("收到:",data.decode())
        connfd.send(b"Thanks/")
    connfd.close()




tcp_socket.close()